﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;

namespace Main.Packets
{
    public class ConnectionPacket : Packet
    {
        private byte adresseSource;
        private byte adresseDestination;

        public ConnectionPacket() { }

        private ConnectionPacket(byte numeroConnexion, PacketType type, byte source, byte destination)
            : base(numeroConnexion, type)
        {
            this.adresseSource = source;
            this.adresseDestination = destination;
        }

        [JsonProperty(Order = 10)]
        public byte AdresseSource
        {
            get { return adresseSource; }
            set { adresseSource = value; }
        }
        [JsonProperty(Order = 20)]
        public byte AdresseDestination
        {
            get { return adresseDestination; }
            set { adresseDestination = value; }
        }

        public static ConnectionPacket CreateRequestConnectionPacket(byte numeroConnexion, byte source, byte destination)
        {
            ConnectionPacket packet = new ConnectionPacket(numeroConnexion, PacketType.CreateTypeForConnectionRequestPacket(), source, destination);

            return packet;
        }

        public static ConnectionPacket CreateConnectionConfirmationPacket(byte numeroConnexion, byte source, byte destination)
        {
            ConnectionPacket packet = new ConnectionPacket(numeroConnexion, PacketType.CreateTypeForConnectionConfirmationPacket(), source, destination);

            return packet;
        }

        public static ConnectionPacket CreateConnectionPacket(string str)
        {
            return JsonConvert.DeserializeObject<ConnectionPacket>(str) as ConnectionPacket;
        }

        public override void WriteJson(System.IO.StreamWriter sw)
        {
            sw.WriteLine(JsonConvert.SerializeObject(this));
        }
    }
}
